Written: 2019-11-24
Last run: 2019-11-27


Visualizations

#for plotting, separate dfs by hemisphere (left, right, commissural)
df_imageLeft <- df_image[df_image$hemisphere == 'left',]
df_imageRight <- df_image[df_image$hemisphere == 'right',]
df_imageCommissural <- df_image[df_image$hemisphere == 'commissural',]

#write a function to have means printed without leading 0
numFormat_fn <- function(val) { 
  sub("^(-?)0.", "\\1.", sprintf("%.3f", val) #3 decimals
  ) 
} 

#write a function to annotate boxplots -- count, mean, and specify position
boxplotStats_fn <- function(y, upper_limit = max(df_image$FA) * 1.1) {
  return( 
    data.frame(
      y = 1 * upper_limit,
      label = paste('n =', length(y), '\n',
                     'x =', numFormat_fn(mean(y)), '\n')
    )
  )
}

#write a function for faceted boxplots
boxplot_fn <- function(df){
ggplot(df, aes(x=group, y=FA), color=group) + 
  geom_boxplot(aes(fill=group, alpha=.7), show.legend = F, outlier.shape = NA) +
  geom_violin(aes(fill=group, alpha=.5), show.legend = F) + 
  geom_jitter(aes(fill=group), colour='black', pch=21, size=2) +
  facet_wrap(~ region , ncol=6) +
  ylim(.15, .7) + #allow room for annotation
  theme_bw() +
  xlab('') +
  theme(legend.position="top") +
  stat_summary(fun.data=boxplotStats_fn, geom='text', hjust=.5, vjust=.9) +
  stat_compare_means(method='t.test',
                     aes(label = paste0('p = ', ..p.format..)))
}                       

Left

Commissural

Data table